package cn.edu.sdjzu.xg.controller;

import cn.edu.sdjzu.xg.domain.Department;
import cn.edu.sdjzu.xg.domain.School;
import cn.edu.sdjzu.xg.domain.Teacher;
import cn.edu.sdjzu.xg.service.DepartmentService;
import cn.edu.sdjzu.xg.service.TeacherService;
import cn.edu.sdjzu.xg.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.SQLException;

@WebServlet("/departmentUpdate.ctl")

public class DepartmentUpdateController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ,ServletException{
        resp.setCharacterEncoding("UTF-8");
        String department_str = JSONUtil.getJSON(req);
        Department department = JSON.parseObject(department_str, Department.class);
        resp.setContentType("application/text;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            DepartmentService.getInstance().update(department);
            message.put("message","修改成功");
            resp.getWriter().println(message);
        } catch (SQLException e){
            e.printStackTrace();
            message.put("message","数据库操作异常");
            resp.getWriter().println(message);
        }catch (Exception e){
            e.printStackTrace();
            message.put("message","网络异常");
            resp.getWriter().println(message);
        }
    }
}

